<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.type" placeholder="类型" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="全部" value="" />
        <el-option label="语音消息" value="1" />
        <el-option label="语音名片" value="2" />
      </el-select>
      <el-select v-model="listQuery.status" placeholder="状态" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="未审核" value="0" />
        <el-option label="已通过" value="1" />
        <el-option label="未通过" value="2" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="七牛任务ID" width="200" align="center">
        <template slot-scope="{row}">
          <span>{{ row.task_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资源" width="150" align="center">
        <template slot-scope="{row}">
          <a class="click_a">点我查看</a>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="80" align="center">
        <template slot-scope="{row}">
          <span>{{ row.type | formatType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="{row}">
          <span>{{ row.status | formatStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="语音文本" min-width="150" align="center">
        <template slot-scope="{row}">
          <span>{{ row.content_txt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获取结果时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updated_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交审核时间" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { voiceMachineAuditList } from '@/api/voiceMachineAudit'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    },
    formatType: function(sex) {
      const sexMap = {
        1: '消息',
        2: '名片'
      }
      return sexMap[parseInt(sex)]
    },
    formatStatus: function(sex) {
      const sexMap = {
        0: '审核中',
        1: '通过',
        2: '存在风险',
        3: '审核不通过'
      }
      return sexMap[parseInt(sex)]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        status: '0',
        sort: 'desc',
        sort_field: 'id',
        user_id: ''
      },
      dialogVisibleRefuse: false,
      refuseId: 0,
      refuse: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      voiceMachineAuditList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = 'id'
      } else {
        this.listQuery.sort = 'id'
      }
      this.handleFilter()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
</style>
